package com.ruoyi.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.domain.AdvInoutstock;
import com.ruoyi.domain.AdvOrdersdetail;
import com.ruoyi.enums.OrderStatusEnum;
import com.ruoyi.service.KuaiDi100Service;
import com.ruoyi.utils.OrderUtils;
import com.ruoyi.utils.SimpleResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.AdvOrders;
import com.ruoyi.service.IAdvOrdersService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * Controller
 *
 * @author ruoyi
 * @date 2024-05-13
 */
@RestController
@RequestMapping("/system/orders")
public class AdvOrdersController extends BaseController {
    @Autowired
    private IAdvOrdersService advOrdersService;

    @Autowired
    private KuaiDi100Service kuaiDi100Service;
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 查询列表
     */

    @PreAuthorize("@ss.hasPermi('system:orders:list')")
    @GetMapping("/list")
    public TableDataInfo list(AdvOrders advOrders) {
        startPage();
        List<AdvOrders> list = advOrdersService.selectAdvOrdersList(advOrders);
        return getDataTable(list);
    }
    /**
     * 小程序查询订单
     */

    @GetMapping("/listByApp")
    public AjaxResult listByApp(AdvOrders advOrders) {
        startPage();
        List<AdvOrders> list = advOrdersService.selectAllByAppOrderList(advOrders);
        return AjaxResult.success(list);
    }


    /**
     * 导出列表
     */

    @PreAuthorize("@ss.hasPermi('system:orders:export')")
    @Log(title = "", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AdvOrders advOrders) {
        List<AdvOrders> list = advOrdersService.selectAdvOrdersList(advOrders);
        ExcelUtil<AdvOrders> util = new ExcelUtil<AdvOrders>(AdvOrders.class);
        util.exportExcel(response, list, "数据");
    }

    /**
     * 获取详细信息
     */

    @PreAuthorize("@ss.hasPermi('system:orders:query')")
    @GetMapping(value = "/{advOrdersId}")
    public AjaxResult getInfo(@PathVariable("advOrdersId") Long advOrdersId) {
        return success(advOrdersService.selectAdvOrdersByAdvOrdersId(advOrdersId));
    }

    /**
     * 获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:orders:query')")
    @GetMapping(value = "/bymembersid/{advMembersId}/{productType}")
    public SimpleResult getInfoOrder(@PathVariable("advMembersId") Long advMembersId,
                                     @PathVariable("productType") int productType) {
        AdvOrders advOrders = advOrdersService.selectAdvOrdersByAdvMembersId(advMembersId, productType);
        return SimpleResult.success(advOrders, "ok");
    }

    /**
     * 获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:orders:query')")
    @GetMapping(value = "/give/{advMembersId}/{productType}")
    public SimpleResult getGive(@PathVariable("advMembersId") Long advMembersId,
                                     @PathVariable("productType") int productType) {
        AdvOrders advOrders = advOrdersService.getGive(advMembersId, productType);
        return SimpleResult.success(advOrders, "ok");
    }

    /**
     * 新增
     */
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('system:orders:add')")
    @Log(title = "", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AdvOrders advOrders) {
        return toAjax(advOrdersService.insertAdvOrders(advOrders));
    }

    @PutMapping(value = "/chosegive/{advOrdersId}/{advProductId}")
    public SimpleResult choseGive(@PathVariable("advOrdersId") Long advOrdersId,
                  @PathVariable("advProductId") Long advProductId){
        int i = advOrdersService.choseGive(advOrdersId, advProductId);
        return SimpleResult.success(i==0?"失败":"成功");
    }

    @PutMapping(value = "/cancelchosegive/{advOrdersId}/{advProductId}")
    public SimpleResult cancelChoseGive(@PathVariable("advOrdersId") Long advOrdersId,
                                  @PathVariable("advProductId") Long advProductId){
        int i = advOrdersService.cancelChoseGive(advOrdersId, advProductId);
        return SimpleResult.success(i==0?"失败":"成功");
    }

    /**
     * 新增
     */
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('system:orders:add')")
    @Log(title = "", businessType = BusinessType.INSERT)
    @PostMapping("/app")
    public AjaxResult addApp(@RequestBody AdvOrders advOrders) {
        return toAjax(advOrdersService.insertAdvOrdersApp(advOrders));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('system:orders:edit')")
    @Log(title = "", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AdvOrders advOrders) {
        System.out.println("修改订单参数 ----> " + advOrders);
        return toAjax(advOrdersService.updateAdvOrders(advOrders));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('system:orders:remove')")
    @Log(title = "", businessType = BusinessType.DELETE)
    @DeleteMapping("/{advOrdersIds}")
    public AjaxResult remove(@PathVariable Long[] advOrdersIds) {
        return toAjax(advOrdersService.deleteAdvOrdersByAdvOrdersIds(advOrdersIds));
    }

    /**
     * 发货
     *
     * @param advOrders
     * @return
     */

    @PostMapping("/sendGoods")
    public AjaxResult sendGoods(@RequestBody AdvOrders advOrders) {
        //新增操作
        if (advOrdersService.selectOutInCount(advOrders.getAdvOrdersId())) {
            String goodsStatus = OrderUtils.chgStatus(OrderStatusEnum.SUCCESS.getType(), "发货");
            advOrders.setOrderStatus(Long.parseLong(goodsStatus));
            String format = simpleDateFormat.format(new Date());
            advOrders.setAdvSenddate(format);
            advOrdersService.updateBySendGoods(advOrders);
            return AjaxResult.success("发货成功");
        } else {
            System.out.println(advOrders);
            return AjaxResult.error("发货失败出入库与订单明细不符");
        }
    }

    /**
     * 查询出库物流编号数组
     * @param advOrdersdetail
     * @return
     */
    @PostMapping("/queryDeviceNo")
    public Map<String, Object> queryDeviceNo(@RequestBody AdvOrdersdetail advOrdersdetail) {

        Map<String, Object> map = advOrdersService.queryDeviceNoByProductId(advOrdersdetail);
        return map;
    }

    /**
      * 出库
     * @param
     * @return
     */

    @PostMapping("/outGoods")
    public AjaxResult outGoods(@RequestBody AdvInoutstock advInoutstock) {
        advOrdersService.exeOutGoods(advInoutstock);
        return AjaxResult.success("出库成功");
    }

    // 确认收货
    @GetMapping("/get/{advOrdersId}")
    public SimpleResult isGet(@PathVariable("advOrdersId") String advOrdersId) {
        boolean result = advOrdersService.isGet(advOrdersId);
        return SimpleResult.success(result?"收货完成":"确认收货失败");
    }

    /**
     * 小程序申请退款
     */

    @PostMapping("/orderRefund")
    public AjaxResult outGoods(@RequestBody AdvOrders advOrders){
        advOrdersService.exeRequestOrderRefund(advOrders);
        return AjaxResult.success("退款已申请");
    }

    /**
     * 小程序退换货申请
     */

    @PostMapping("/goodsChangeRequest")
    public AjaxResult goodsChangeRequest(@RequestBody AdvOrders advOrders){
        advOrdersService.goodsChangeRequest(advOrders);
        System.out.println(advOrders);
        return AjaxResult.success("申请成功");
    }

    /**
     * 退换货操作
     */

    @PostMapping("/goodsChange")
    public AjaxResult goodsChange(@RequestBody AdvOrders advOrders){
        advOrdersService.exeGoodsChange(advOrders);
        System.out.println(advOrders);
        return AjaxResult.success();
    }

    /**
     * 更新订单数量
     */

    @PostMapping("/updateByNum")
    public AjaxResult updateByNum(@RequestBody List<AdvOrders> advOrders){
        advOrdersService.updateOrdersNum(advOrders);
        return AjaxResult.success();
    }

}
